// <copyright file="OCLInstruction.cs" company="Codeplex project Eco.Contrib"> 
// The MIT License (MIT) 
// </copyright> 
// <author>Peter Buchmann</author> 

namespace Eco.Contrib
{
    using System;
    using System.Collections.Generic;
    using Eco.ObjectRepresentation;
    using Eco.Services;
    using Eco.UmlCodeAttributes;

    /// <summary>
    /// OCLInstruction: Instruction that represents a OCL query
    /// </summary>
    public partial class OclInstruction
    {
        /// <summary>
        /// Executes the function.
        /// </summary>
        /// <param name="serviceProvider">The service provider.</param>
        /// <param name="variableList">The variable list.</param>
        /// <returns>Return value of executed function</returns>
        [UmlElement(Id = "5b02a342-4375-44a0-969d-0d815b88cfc6")]
        protected internal override IElement ExecuteFunction(IEcoServiceProvider serviceProvider, IModifiableVariableList variableList)
        {
            IOclService oclService = serviceProvider.GetEcoService<IOclService>();
            IElement element = oclService.Evaluate(OCL, variableList);
            SetVariable(serviceProvider, variableList, element);
            return element;
        }

        /// <summary>
        /// Inits the specified tokens.
        /// </summary>
        /// <param name="tokens">The tokens.</param>
        [UmlElement(Id = "fb5c717b-e022-4d33-873e-a6ea5624060d")]
        protected internal override void Init(Dictionary<IList<EslToken>, Type> tokens)
        {
            List<EslToken> tokenList = new List<EslToken>();
            tokenList.Add(new EslToken(EslTokenType.Word, "Ocl"));
            tokenList.Add(new EslToken(EslTokenType.Operator, "("));
            tokens.Add(tokenList, this.GetType());
            tokenList = new List<EslToken>();
            tokenList.Add(new EslToken(EslTokenType.Word, "%"));
            tokenList.Add(new EslToken(EslTokenType.Operator, ":="));
            tokenList.Add(new EslToken(EslTokenType.Word, "Ocl"));
            tokenList.Add(new EslToken(EslTokenType.Operator, "("));
            tokens.Add(tokenList, this.GetType());
        }

        /// <summary>
        /// Parses the specified token list.
        /// </summary>
        /// <param name="tokenList">The token list.</param>
        /// <param name="ecoScript">The eco script.</param>
        /// <param name="fullExpression">if set to <c>true</c> [full expression].</param>
        [UmlElement(Id = "96da73f0-aa5b-4225-bb94-c9bd1a2986ba")]
        protected internal override void Parse(IList<EslToken> tokenList, EcoScript ecoScript, bool fullExpression)
        {
            EslToken token = GetNextToken(tokenList);
            RememberEcoScript(ecoScript, token);
            EslToken tokenOperator = GetNextToken(tokenList);

            if ((token.TokenType == EslTokenType.Word) &&
                 (tokenOperator.TokenType == EslTokenType.Operator) && (tokenOperator.TokenValue == ":="))
            {
                ResultVariable = new Variable(this.ServiceProvider());
                ResultVariable.Name = token.TokenValue;
                token = GetNextToken(tokenList);
                tokenOperator = GetNextToken(tokenList);
            }

            EslToken firstToken = null;
            int openParenthesis = 1;
            do 
            {
                token = GetNextToken(tokenList);
                if (firstToken == null)
                {
                    firstToken = token;
                }

                if (token.TokenType == EslTokenType.Operator)
                {
                    if (token.TokenValue == "(")
                    {
                        openParenthesis++;
                    }

                    if (token.TokenValue == ")")
                    {
                        openParenthesis--;
                    }
                }
            } 
            while (openParenthesis > 0);

            OCL = ecoScript.GetTextBetweenTokens(firstToken, MyTokens[MyTokens.Count - 1]);
            
            if (fullExpression)
            {
                EslToken tokenEnd = GetNextToken(tokenList);
                AssertInstructionEnd(tokenEnd);
            }
        }
    }
}